/* exif_main.c - 调用add_exif_data函数的主程序 */

#include "exif_lib.h"
#include <stdio.h>
#include <stdlib.h>

// 声明add_exif_data函数
void add_exif_data(const char *filename);

int main(int argc, char *argv[])
{
    const char *filename = "test_image.jpg";

    // 如果提供了命令行参数，则使用第一个参数作为文件名
    if (argc > 1) {
        filename = argv[1];
    }

    printf("[MAIN] 开始添加EXIF数据到文件: %s\n", filename);
    add_exif_data(filename);
    printf("[MAIN] 操作完成\n");

    return 0;
}

// add_exif_data函数实现
void add_exif_data(const char *filename)
{
    printf("[EXIF] step 0: check filename ptr\n");
    if (!filename) {
        printf("[EXIF] filename is NULL!\n");
        return;
    }
    printf("[EXIF] filename = %s\n", filename);
    FILE *fp = fopen(filename, "rb");
    if (!fp) {
        printf("[EXIF] file not exist or open failed: %s\n", filename);
        return;
    } else {
        printf("[EXIF] file open ok: %s\n", filename);
        fclose(fp);
    }

    printf("[EXIF] step 1: exif_create\n");
    ExifData *exif = exif_create();
    if (!exif) {
        printf("[EXIF] exif_create failed\n");
        return;
    }
    printf("[EXIF] step 2: add make\n");
    exif_add_string(exif, IFD0, EXIF_TAG_MAKE, "Cannice");
    printf("[EXIF] step 3: add model\n");
    exif_add_string(exif, IFD0, EXIF_TAG_MODEL, "linker01");
    printf("[EXIF] step 4: add iso\n");
    exif_add_short(exif, EXIF_IFD, EXIF_TAG_ISO_SPEED, 800);
    printf("[EXIF] step 5: add gps\n");
    exif_add_gps(exif, 39.9087, 116.3975, 43.5);
    printf("[EXIF] step 6: generate exif\n");
    if (!exif_generate(exif)) {
        printf("[EXIF] exif_generate failed\n");
        exif_destroy(exif);
        return;
    }
    printf("[EXIF] step 7: insert exif to jpeg\n");
    int insert_ret = exif_insert_to_jpeg(filename, exif);
    printf("[EXIF] exif_insert_to_jpeg return: %d\n", insert_ret);
    if (insert_ret) {
        printf("[EXIF] insert exif success: %s\n", filename);
    } else {
        printf("[EXIF] insert exif failed\n");
    }
    printf("[EXIF] step 8: destroy\n");
    exif_destroy(exif);
    printf("[EXIF] done\n");
}